﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Library;

namespace Sport_Shop
{
    public partial class KQTim : System.Web.UI.Page
    {

        static PagedDataSource p = new PagedDataSource();
        public static int intSTT;
        public static int trang_thu = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                trang_thu = 0;
                load_KQTim();

            }
        }

        private void load_KQTim()
        {
            try
            {
                List<Library.SanPham> result = new List<Library.SanPham>();

                if (Request.QueryString["TenSP"] != "")
                {
                    string tensp = Request.QueryString["TenSP"].ToString();
                    result = Library.SanPham.TimKiemSP(tensp, 0, Int32.MaxValue);

                }//Tìm theo TenSP

                if (Request.QueryString["TenSP"] == "" && Request.QueryString["GiaTu"] != "" && Request.QueryString["GiaDen"] != "")
                {
                    string giatu = Request.QueryString["GiaTu"].ToString();
                    string giaden = Request.QueryString["GiaDen"].ToString();
                    result = Library.SanPham.TimKiemSP("", Convert.ToInt32(giatu), Convert.ToInt32(giaden));
                }//Tim theo Gia


                if (Request.QueryString["TenSP"] != "" && Request.QueryString["GiaTu"] != "" && Request.QueryString["GiaDen"] != "")
                {
                    string tensp = Request.QueryString["TenSP"].ToString();
                    string giatu = Request.QueryString["GiaTu"].ToString();
                    string giaden = Request.QueryString["GiaDen"].ToString();
                    result = Library.SanPham.TimKiemSP(tensp, Convert.ToInt32(giatu), Convert.ToInt32(giaden));
                }//Tim theo Gia & Ten

                p.DataSource = result;
                p.PageSize = 4;
                p.CurrentPageIndex = trang_thu;
                p.AllowPaging = true;

                btDau.Enabled = true; btTruoc.Enabled = true; btSau.Enabled = true; btCuoi.Enabled = true;

                if (p.IsFirstPage == true)
                {
                    btDau.Enabled = false;
                    btTruoc.Enabled = false;
                    btSau.Enabled = true;
                    btCuoi.Enabled = true;
                }
                if (p.IsLastPage == true)
                {
                    btDau.Enabled = true;
                    btTruoc.Enabled = true;
                    btSau.Enabled = false;
                    btCuoi.Enabled = false;
                }
                tbTrang.Text = (trang_thu + 1) + "/" + p.PageCount;
                dlKQTim.DataSource = p;
                dlKQTim.DataBind();

            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            
        }

        protected void btDau_Click(object sender, EventArgs e)
        {
            trang_thu = 0;
            load_KQTim();
        }

        protected void btTruoc_Click(object sender, EventArgs e)
        {
            trang_thu--;
            load_KQTim();
        }

        protected void btSau_Click(object sender, EventArgs e)
        {
            trang_thu++;
            load_KQTim();
        }

        protected void btCuoi_Click(object sender, EventArgs e)
        {
            trang_thu = p.PageCount - 1;
            load_KQTim();
        }


    }
}

            
    